/**
 * Created by Luji on 2017/02/03.
 */
class Delete {
    constructor(appSrv,ctgNotification,treeData,souceName,editRootNode,$modalInstance) {
        "ngInject";
        this._appSrv        = appSrv;
        this._notification  = ctgNotification;
        this._modalInstance = $modalInstance;
        this.table          = [];
        this.treeData       = treeData;
        this.souceName      = souceName;
        this.children       = [];
        this.allNodes       = [];
        this.checkedList    = [];
        this.uncheckList    =[];
        this.editRootNode   = editRootNode;
        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null
        };

        // 遍历出该source下的子节点
        this.findChildNodes(this.treeData);
        // 全部子节点
        this.findAllNodes(this.treeData)

        this.getList();

    }
    findChildNodes(data,targetName){
        if(this.editRootNode){
            this.checkedList = [this.souceName];
            angular.forEach(data, (ele,index)=>{
                if(ele.name == this.souceName){
                    angular.forEach(ele.children, (child,index)=>{
                        this.children.push(child.name);
                    })
                }else{
                    this.findChildNodes(ele.children)
                }
            })
        }else{
            angular.forEach(data, (ele,index)=>{
                if(ele.name == this.souceName){
                    angular.forEach(ele.children, (child,index)=>{
                        this.checkedList.push(child.name);
                        this.children.push(child.name);
                    })
                }else{
                    this.findChildNodes(ele.children)
                }
            })
        }

    }
    findAllNodes(data){
        angular.forEach(data, (ele,index)=>{
            this.allNodes.push(ele.name)
            if(ele.children && ele.children.length!=0){
                this.findAllNodes(ele.children)
            }
        })
    }
    getList(){
        this.table = null;
        this.uncheckList = [];
        this.loading = true;

        this._appSrv.getAppList({currentPage: this.page.currentPage}, this.filter).then(data=> {
            this.loading = false;
            if(data){
                _.each(data.list, (ele,index)=>{
                    if(_.indexOf(this.allNodes, ele.appCode)<0){
                        this.uncheckList.push(ele.appCode);
                    }
                });
            }

        }).catch(()=> {
            this._notification.error('获取服务依赖数据有误.');
        });
    }
    cancel() {
        this._modalInstance.dismiss('cancel');
    }

    save() {
        if(this.editRootNode){
            this._modalInstance.close({rootNode:this.checkedList[0]});
        }else{
            this._modalInstance.close({addNodes:_.difference(this.checkedList,this.children),delNodes:_.difference(this.children,this.checkedList)});
        }


    }

    toggleCheck(type,item){
        if(this.treeData.length==0 || this.editRootNode){
            // 新建根节点（唯一）
            if(type == 'check'){
                let idx      = _.indexOf(this.uncheckList, item);
                if(this.checkedList[0]){
                    this.uncheckList.push(this.checkedList[0])
                }
                this.checkedList = [];
                this.checkedList.push(item);
                this.uncheckList.splice(idx, 1)
            }else{
                let idx      = _.indexOf(this.checkedList, item);
                this.uncheckList.push(item)
                this.checkedList.splice(idx, 1);
            }
        }else{
            // 编辑节点
            if(type == 'check'){
                let idx      = _.indexOf(this.uncheckList, item);
                this.checkedList.push(item);
                this.uncheckList.splice(idx, 1);
            }else{
                let idx      = _.indexOf(this.checkedList, item);
                this.uncheckList.push(item)
                this.checkedList.splice(idx, 1);
            }
        }
    }
    search() {
        this.page.currentPage = 1;
        this.getList();
    }
}
export default Delete